/**
 * Copyright: Copyright (c)2011
 * Company: 易宝支付(YeePay)
 */
package net.loyintean.pearlshell.validation.rule;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import net.loyintean.pearlshell.validation.IValidRuleConfig;

/** @author：jun.lin@yeepay.com
 * @since：2014年12月18日 下午1:27:31
 * @version: */
class StringInDateformatValidRuleImpl extends AbstractStringValidRuleImpl {

	/** @see net.loyintean.pearlshell.validation.IValidRule#errorMsg(net.loyintean.pearlshell.validation.IValidRuleConfig) */
	@Override
	public String errorMsg(IValidRuleConfig paramIValidRuleConfig) {
		return "不符合日期格式" + paramIValidRuleConfig.validParam()[0];
	}

	/** @see net.loyintean.pearlshell.validation.rule.AbstractStringValidRuleImpl#validString(java.lang.String,
	 *      net.loyintean.pearlshell.validation.IValidRuleConfig) */
	@Override
	protected boolean validString(String date,
			IValidRuleConfig paramIValidRuleConfig) {
		assert paramIValidRuleConfig != null;
		assert paramIValidRuleConfig.validParam() != null;

		if (paramIValidRuleConfig.validParam().length > 0) {
			String format = (String) paramIValidRuleConfig.validParam()[0];
			try {
				new SimpleDateFormat(format).parse(date);
			} catch (ParseException e) {
				return false;
			}
		} else {
			throw new IllegalArgumentException("对日期格式进行校验时，请指定日期格式！");
		}

		// pass the validation if nothing happend former.
		return true;
	}

}
